﻿using Swsk33.ReadAndWriteSharp.Util;
using System.IO;

namespace Swsk33.FinalCourseDesign.Model
{
	/// <summary>
	/// 配置信息类-单例模式
	/// </summary>
	public class Configure
	{
		private string mapResource;

		private string server;

		/// <summary>
		/// 地图资源位置
		/// </summary>
		public string MapResource { get => mapResource; set => mapResource = value; }

		/// <summary>
		/// 后端服务端地址
		/// </summary>
		public string Server { get => server; set => server = value; }

		private Configure() { }

		private static Configure configure = new Configure();

		/// <summary>
		/// 获取唯一配置类实例
		/// </summary>
		/// <returns>配置信息</returns>
		public static Configure GetConfigure()
		{
			return configure;
		}

		/// <summary>
		/// 读取配置文件
		/// </summary>
		public void ReadConfig()
		{
			string[] configLines = File.ReadAllLines(@"resource\config\main.properties");
			// 解析配置
			foreach (string line in configLines)
			{
				string trimLine = line.Trim();
				if (trimLine.StartsWith("#"))
				{
					continue;
				}
				if (trimLine.StartsWith("map-resource="))
				{
					mapResource = trimLine.Substring(trimLine.IndexOf("=") + 1);
				}
				else if (trimLine.StartsWith("server="))
				{
					server = trimLine.Substring(trimLine.IndexOf("=") + 1);
				}
			}
			// 处理一下服务端地址
			server = FilePathUtils.RemovePathEndSlash(server);
		}
	}
}